package com.city.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.city.eduservice.entity.EduChapter;
import com.city.eduservice.entity.EduCourse;
import com.city.eduservice.entity.EduCourseDescription;
import com.city.eduservice.entity.vo.CourseInfoVo;
import com.city.eduservice.entity.vo.CoursePublishVo;
import com.city.eduservice.mapper.EduCourseMapper;
import com.city.eduservice.service.EduChapterService;
import com.city.eduservice.service.EduCourseDescriptionService;
import com.city.eduservice.service.EduCourseService;
import com.city.eduservice.service.EduVideoService;
import com.city.servicebase.myexception.MyException;
import com.city.servicebase.myexception.exceptionenum.MyExceptionEnumImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author city
 * @since 2020-07-01
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        /*数据库添加课程信息*/
        EduCourse eduCourse = new EduCourse();
        //对象赋值方法(非空对象，被赋值对象)
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert==0){
            throw new MyException(MyExceptionEnumImpl.COURSE_INFO_FAILURE);
        }

        /*数据库添加课程简介*/
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        //给描述和简介一对一关系关联，不使用外键，使用相同的id来对应关系
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescriptionService.save(eduCourseDescription);
        return eduCourse.getId();
    }

    //前端添加课程后上一步实现数据查询回显，然后修改数据
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        EduCourseDescription byId = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(byId.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.updateById(eduCourse);
        if (i==0){
            throw new MyException(MyExceptionEnumImpl.UPDATE_COURSE_FAILURE);
        }
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //通过自己写的sql查询组合数据
        //通过baseMapper，里面关联了mapperxml的接口
        CoursePublishVo coursePublishInfo = baseMapper.getCoursePublishInfo(id);
        return coursePublishInfo;
    }

    @Override
    public void removeCorse(String courseId) {
        //先删小节，阿里云视频
        eduVideoService.removeVideoByCourseId(courseId);
        //删章节,可以在这删，最好在service中新建方法
        QueryWrapper<EduChapter> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        eduChapterService.remove(queryWrapper);
        //删描述-描述id和课程id一样
        eduCourseDescriptionService.removeById(courseId);
        //删课程本身
        int i = baseMapper.deleteById(courseId);
        //如果i=0证明删除失败，原因可能为他的一对多，外键没删除干净，如果关联了物理外键的话
        if (i==0){
            throw new MyException(MyExceptionEnumImpl.NOT_DELETE_COURSE);
        }
    }

    @Cacheable(key = "'selectCourseList'",value = "EightCourse")
    @Override
    public List<EduCourse> getEightCourse() {

        QueryWrapper<EduCourse> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 8");
        List<EduCourse> list = baseMapper.selectList(queryWrapper);
        return list;
    }
}
